package com.fulihui.usercore.service;

import com.fulihui.usercore.api.MerchantService;
import com.fulihui.usercore.common.util.Errors;
import com.fulihui.usercore.condition.MerchantCondition;
import com.fulihui.usercore.core.repository.MerchantRepository;
import com.fulihui.usercore.dto.MerchantDTO;
import com.fulihui.usercore.enums.MerchantStateEnum;
import com.fulihui.usercore.request.merchant.MerchantSaveRequest;
import com.fulihui.usercore.request.merchant.UpdateStateRequest;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.InvokeServiceException;
import org.near.servicesupport.request.TPageRequest;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 *
 * Created by Willard.Hu on 2016/5/17.
 */
@Service(version = "1.0.0")
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    private MerchantRepository merchantRepository;

    /**
     * @see MerchantService#save(MerchantSaveRequest)
     */
    @Override
    public TSingleResult<String> save(MerchantSaveRequest request) {
        String mid = request.getMid();
        if (StringUtil.isBlank(request.getMid())) {
            // 新增时如果状态未传，默认给正常状态
            if (StringUtil.isBlank(request.getState())) {
                request.setState(MerchantStateEnum.NOMAL.getCode());
            }
            checkMerchantName(request.getName(), null);
            mid = merchantRepository.insert(request);
        } else {
            if (StringUtil.isNotBlank(request.getName())) {
                checkMerchantName(request.getName(), request.getMid());
            }
            boolean b = merchantRepository.update(request);
            if (!b) {
                return ResultBuilder.failTSingle(Errors.Merchant.MERCHANT_OPERATE_ERROR);
            }
        }
        return ResultBuilder.succTSingle(mid);
    }

    /**
     * 验证商户名称是否有重复
     * @param name 商户名称
     * @param mid 商户唯一标识
     */
    private void checkMerchantName(String name, String mid) {
        // 按名称查询商户数据时，忽略状态
        // XXX 以后考虑删除状态的数据如何处理
        MerchantDTO m = merchantRepository.queryByName(name, null);
        // 有同名商户，并且商户不是当前商户(更新时的校验)
        if (m != null && !StringUtil.equals(m.getMid(), mid)) {
            throw new InvokeServiceException(Errors.Merchant.MERCHANT_NAME_HAS_EXISTS);
        }
    }

    /**
     * @see MerchantService#updateState(UpdateStateRequest)
     */
    @Override
    public BaseResult updateState(UpdateStateRequest request) {
        boolean b = merchantRepository.updateState(request);
        if (!b) {
            return ResultBuilder.failTSingle(Errors.Merchant.MERCHANT_OPERATE_ERROR);
        }
        return ResultBuilder.succ(BaseResult.class);
    }

    /**
     * @see MerchantService#queryByPK(TRequest)
     */
    @Override
    public TSingleResult<MerchantDTO> queryByPK(TRequest<String> request) {
        MerchantDTO dto = merchantRepository.queryByPK(request.getCondition());
        return ResultBuilder.succTSingle(dto);
    }

    /**
     * @see MerchantService#queryPage(TPageRequest)
     */
    @Override
    public TPageResult<MerchantDTO> queryPage(TPageRequest<MerchantCondition> request) {
        List<MerchantDTO> dtoList = merchantRepository.queryPage(request.start4Mysql(),
            request.getRows(), request.getCondition());
        int totalCount = merchantRepository.totalCount(request.getCondition());
        return ResultBuilder.succTPage(dtoList, request.getPage(), request.getRows(), totalCount);
    }
}
